<?php

namespace common\models\info;

use common\component\BaseActiveRecord;
use Faker\Provider\Text;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\Url;

/**
 * This is the model class for table "{{%zt_mood}}".
 *
 *  @property string $id
 *  @property string $created_at 创建时间
 *  @property Text $content 内容
 */
class Leave extends BaseActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%zt_mood}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [[ 'created_at'], 'integer'],
            [['content'], 'text'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '序列',
            'content' => '内容',
            'created_at' => '创建时间',
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    self::EVENT_BEFORE_INSERT => ['created_at']
                ]
            ]
        ];
    }

    /**
     * 保存数据之前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if (!parent::beforeSave($insert)) {
            return false;
        }

        if (!$insert) {
            $where_one = ['and'];
            $where_one[] = ['!=', 'id', $this->id];
            if (static::find()->where($where_one)->exists()) {
                $this->addError("mood", "【心情】已存在");
                return false;
            }
        }
    }


    public function afterFind()
    {
    }

    /**
     * 实例化模型对像
     * @param $id
     * @return BaseActiveRecord|null|static
     */
    public static function modelById($id) {
        if (!$id) {
            $model = new self();
        }
        else {
            $model = static::findOne($id);
        }
        return $model;
    }
    public static function getMood($page,$pageSize){
        $q = static::find()->select("id,content,created_at")->orderBy("created_at DESC");
        $data = self::arrListByPage($q,$page,$pageSize);
        return $data;

    }
}
